package com.tianlu.store.utils;

import cn.hutool.core.util.StrUtil;
import com.tianlu.store.common.ResultCode;
import com.tianlu.store.common.ServiceException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Objects;

/**
 * @author liangdl
 * @since 2023/4/21 20:54
 */
public class JwtUtils {
    public static final long EXPIRE = 86400000L;
    public static final String APP_SECRET = "6a040886f3284b9cace25e7c6336b724";

    public JwtUtils() {
    }

    public static String getJwtToken(Long id, String openId, String name, String phone, Integer status, Integer level) {
        return Jwts.builder().setHeaderParam("typ", "JWT").setHeaderParam("alg", "HS256").setSubject("jacob-user").setIssuedAt(new Date()).setExpiration(new Date(System.currentTimeMillis() + EXPIRE)).claim("id", id).claim("openId", openId).claim("name", name).claim("phone", phone).claim("status", status).claim("level", level).signWith(SignatureAlgorithm.HS256, APP_SECRET).compact();
    }

    public static boolean checkToken(String jwtToken) {
        if (StrUtil.isEmpty(jwtToken)) {
            return false;
        } else {
            try {
                Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
                return true;
            } catch (Exception var2) {
                var2.printStackTrace();
                return false;
            }
        }
    }

    public static boolean checkToken() {
        return checkToken(ServletsUtils.getToken(false));
    }

    public static String getSaltByJwtToken() {
        try {
            String jwtToken = ServletsUtils.getToken(false);
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
            Claims claims = (Claims) claimsJws.getBody();
            return claims.get("salt").toString();
        } catch (Exception var3) {
            var3.printStackTrace();
            return "";
        }
    }

    public static Long getIdByJwtToken() {
        String jwtToken = ServletsUtils.getToken(false);
        if (StrUtil.isEmpty(jwtToken)) {
            throw new ServiceException(ResultCode.CODE_401);
        } else {
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
            Claims claims = (Claims) claimsJws.getBody();
            return Long.parseLong(claims.get("id").toString());
        }
    }

    public static Long getOpenIdByJwtToken() {
        String jwtToken = ServletsUtils.getToken(false);
        if (StrUtil.isEmpty(jwtToken)) {
            throw new ServiceException(ResultCode.CODE_401);
        } else {
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
            Claims claims = (Claims) claimsJws.getBody();
            return Long.parseLong(claims.get("openId").toString());
        }
    }

    public static String getNameByJwtToken() {
        String jwtToken = ServletsUtils.getToken(false);
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        Claims claims = (Claims) claimsJws.getBody();
        Object accountObj = claims.get("name");
        return Objects.nonNull(accountObj) ? accountObj.toString() : null;
    }

    public static String getPhoneByJwtToken() {
        String jwtToken = ServletsUtils.getToken(false);
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        Claims claims = (Claims) claimsJws.getBody();
        Object accountObj = claims.get("phone");
        return Objects.nonNull(accountObj) ? accountObj.toString() : null;
    }

    public static Integer getLevelByJwtToken() {
        String jwtToken = ServletsUtils.getToken(false);
        if (StrUtil.isEmpty(jwtToken)) {
            throw new ServiceException(ResultCode.CODE_401);
        } else {
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
            Claims claims = (Claims) claimsJws.getBody();
            return Integer.parseInt(claims.get("level").toString());
        }
    }
}
